//
//  JBNewFeatureCollectionViewController.swift
//  sinaBlog
//
//  Created by jobo on 15/11/1.
//  Copyright © 2015年 jobo. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class JBNewFeatureCollectionViewController: UICollectionViewController {

    let count = 4
    
    var layout = UICollectionViewFlowLayout()
    
    init()
    {
        super.init(collectionViewLayout:layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView!.registerClass(JBNewFeatrueVcCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        prepareForLayout()
    
    }
    
    
    //准备ui
    func prepareForLayout()
    {
        //设置item的size
        layout.itemSize = UIScreen.mainScreen().bounds.size
        //设置内间距为0
        layout.minimumInteritemSpacing = 0
        //设置外间距为0
        layout.minimumLineSpacing = 0
        //设置滚动的方向
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //设置页面切换
        collectionView?.pagingEnabled = true
        //取消弹簧效果
        collectionView?.bounces = false
    }
    


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: UICollectionViewDataSource

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! JBNewFeatrueVcCell
     
        // Configure the cell
        cell.backgroundColor = UIColor.whiteColor()
        
        cell.imageIndex = indexPath.item
       
        return cell
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        // 正在显示的cell的indexPath
        let showIndexPath = collectionView.indexPathsForVisibleItems().first!
        
        // 获取collectionView正在显示的cell
        let cell = collectionView.cellForItemAtIndexPath(showIndexPath) as! JBNewFeatrueVcCell
        
        // 最后一页动画
        if showIndexPath.item == count - 1 {
            // 开始按钮动画
            cell.startBtnAnima()
        }
    }


}

class JBNewFeatrueVcCell : UICollectionViewCell
{
    
    
    var imageIndex: Int = 0 {
        didSet {
            // 知道当前是哪一页
            // 设置图片
            backgroundImgView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            
            startBtn.hidden = true
        }
    }
    
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareForUI()
    }
    
    //startBtn animation 
    func startBtnAnima()
    {
        startBtn.hidden = false
        
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        //参数1 : 时间
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
        }
        
        
    }
    
    
    //准备ui
    func prepareForUI()
    {
        
        
        contentView.addSubview(backgroundImgView)
        
        
        backgroundImgView.translatesAutoresizingMaskIntoConstraints = false
        
        contentView.addConstraints( NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backimg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["backimg" : backgroundImgView]))
        contentView.addConstraints( NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backimg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["backimg" : backgroundImgView]))
        
        contentView.addSubview(startBtn)

    
        startBtn.translatesAutoresizingMaskIntoConstraints = false

        
        contentView.addConstraint( NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        
        contentView.addConstraint( NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
    }
    
    lazy var backgroundImgView = UIImageView()
    
    lazy var startBtn : UIButton =
    {
        var btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setTitle("开始", forState: UIControlState.Normal)
        
        //按钮点击事件
        btn.addTarget(self, action: "startBtnClick", forControlEvents: UIControlEvents.TouchUpInside)

        
        return btn
    }()
    
    
    //响应按钮点击事件
    func startBtnClick()
    {
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchController(true)
        print("开始")
    }
    
   // func accessibilityActivate() -> Bool {
       // <#code#>
    //}
}







